
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Linq.Extensions;
using Abp.Extensions;
using Abp.Domain.Repositories;
using L._52ABP.Application.Dtos;
using L._52ABP.Common.Extensions.Enums;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

using Yozeev.BusinessLogic;
using Yozeev.BusinessLogic.Dtos;
using Yozeev.BusinessLogic.Exporting;
using Yozeev.BusinessLogic.DomainService;
using Yozeev.BusinessLogic.Authorization;

namespace Yozeev.BusinessLogic
{
    /// <summary>
    /// 应用层服务的接口实现方法
    ///</summary>
    [AbpAuthorize]
    public class TicketRoleAppService : YozeevAppServiceBase, ITicketRoleAppService
    {
        private readonly IRepository<TicketRole, Guid> _ticketRoleRepository;
		private readonly ITicketRoleListExcelExporter _ticketRoleListExcelExporter;
		private readonly ITicketRoleManager _ticketRoleManager;
        /// <summary>
        /// 构造函数
        ///</summary>
        public TicketRoleAppService(
		IRepository<TicketRole, Guid>  ticketRoleRepository
              ,ITicketRoleManager ticketRoleManager       
,TicketRoleListExcelExporter ticketRoleListExcelExporter                                                                                                       
             )
            {
            _ticketRoleRepository = ticketRoleRepository;
             _ticketRoleManager=ticketRoleManager;;
_ticketRoleListExcelExporter = ticketRoleListExcelExporter;            
            }


            /// <summary>
                /// 获取的分页列表信息
                ///      </summary>
            /// <param name="input"></param>
            /// <returns></returns>
            [AbpAuthorize(TicketRolePermissions.Query)]
            public async Task<PagedResultDto<TicketRoleListDto>> GetPaged(GetTicketRolesInput input)
		{

		    var query = _ticketRoleRepository.GetAll()

			;
			// TODO:根据传入的参数添加过滤条件

			var count = await query.CountAsync();

			var ticketRoleList = await query
					.OrderBy(input.Sorting).AsNoTracking()
					.PageBy(input)
					.ToListAsync();

		  var ticketRoleListDtos = ObjectMapper.Map<List<TicketRoleListDto>>(ticketRoleList);

			return new PagedResultDto<TicketRoleListDto>(count,ticketRoleListDtos);
		}


		/// <summary>
		/// 通过指定id获取TicketRoleListDto信息
		/// </summary>
		[AbpAuthorize(TicketRolePermissions.Query)]
		public async Task<TicketRoleListDto> GetById(EntityDto<Guid> input)
		{
			var entity = await _ticketRoleRepository.GetAsync(input.Id);

			var dto = ObjectMapper.Map<TicketRoleListDto>(entity);
			return dto;
 		}

		/// <summary>
		/// 获取编辑 
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[AbpAuthorize(TicketRolePermissions.Create,TicketRolePermissions.Edit)]
		public async Task<GetTicketRoleForEditOutput> GetForEdit(NullableIdDto<Guid> input)
		{
			var output = new GetTicketRoleForEditOutput();
TicketRoleEditDto editDto;

			if (input.Id.HasValue)
			{
				var entity = await _ticketRoleRepository.GetAsync(input.Id.Value);
				editDto = ObjectMapper.Map<TicketRoleEditDto>(entity);
			}
			else
			{
				editDto = new TicketRoleEditDto();
			}
    


            output.TicketRole = editDto;
			return output;
		}


		/// <summary>
		/// 添加或者修改的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[AbpAuthorize(TicketRolePermissions.Create,TicketRolePermissions.Edit)]
		public async Task CreateOrUpdate(CreateOrUpdateTicketRoleInput input)
		{

			if (input.TicketRole.Id.HasValue)
			{
				await Update(input.TicketRole);
			}
			else
			{
				await Create(input.TicketRole);
			}
		}


		/// <summary>
		/// 新增
		/// </summary>
		[AbpAuthorize(TicketRolePermissions.Create)]
		protected virtual async Task<TicketRoleEditDto> Create(TicketRoleEditDto input)
		{
			//TODO:新增前的逻辑判断，是否允许新增

            var entity = ObjectMapper.Map<TicketRole>(input);
            //调用领域服务
            entity = await _ticketRoleManager.CreateAsync(entity);

            var dto=ObjectMapper.Map<TicketRoleEditDto>(entity);
            return dto;
		}

		/// <summary>
		/// 编辑
		/// </summary>
		[AbpAuthorize(TicketRolePermissions.Edit)]
		protected virtual async Task Update(TicketRoleEditDto input)
		{
			//TODO:更新前的逻辑判断，是否允许更新

		 var entity = await _ticketRoleRepository.GetAsync(input.Id.Value);
          //  input.MapTo(entity);
          //将input属性的值赋值到entity中
             ObjectMapper.Map(input, entity);
            await _ticketRoleManager.UpdateAsync(entity);
		}



		/// <summary>
		/// 删除信息
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[AbpAuthorize(TicketRolePermissions.Delete)]
		public async Task Delete(EntityDto<Guid> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
            await _ticketRoleManager.DeleteAsync(input.Id);
		}



		/// <summary>
		/// 批量删除TicketRole的方法
		/// </summary>
		[AbpAuthorize(TicketRolePermissions.BatchDelete)]
		public async Task BatchDelete(List<Guid> input)
		{
			// TODO:批量删除前的逻辑判断，是否允许删除
            await _ticketRoleManager.BatchDelete(input);
		}



		
		/// <summary>
		/// 导出为excel文件
		/// </summary>
		/// <returns></returns>
		[AbpAuthorize(TicketRolePermissions.ExportExcel)]
		public async Task<FileDto> GetToExcelFile()
		{
		   var ticketRoles = await _ticketRoleManager.QueryTicketRoles().ToListAsync();
            var ticketRoleListDtos = ObjectMapper.Map<List<TicketRoleListDto>>(ticketRoles);
            return _ticketRoleListExcelExporter.ExportToExcelFile(ticketRoleListDtos);
		}

		

							//// custom codes



							//// custom codes end

    }
}


